package organization

import (
	"awesomeProject1/models/pojo"
	"awesomeProject1/pkg/e"
	"awesomeProject1/pkg/gintool"
	"strings"
	"xorm.io/xorm"
)

type Service interface {

	// GetOrganizationByID 通过机构编号精确查询机构
	GetOrganizationByID(search string, pager *gintool.Pager) (list []models.Organization, total int64, code int)
	// GetOrganizationByName 通过机构名精确查询机构
	GetOrganizationByName(search string, pager *gintool.Pager) (list []models.Organization, total int64, code int)

	// GetOrganizationList 通过机构名关键字查询机构
	GetOrganizationList(search string, pager *gintool.Pager) (list models.OrganizationList, total int64, code int)

	// GetOrganizationListFromParentOrg 通过机构名关键字查询机构和下属机构
	GetOrganizationListFromParentOrg(orgID string, pager *gintool.Pager) (list models.OrganizationList, total int64, code int)

	CreateOrganization(organization models.Organization) int
	UpdateOrganization(organization *models.Organization) int
	// GetOrgListByIds 通过机构编号获取机构名称
	GetOrgListByIds(orgCodes []string) (list models.OrganizationList, code int)
	// GetOrgNamesByParent 查询机构和下属机构名称
	GetOrgNamesByParent(orgId string) (list models.OrganizationList, code int)

	ReadOrgListByIds(ids []uint32) (models.OrganizationList, error)
}

type service struct {
	db *xorm.Engine
}

func New(db *xorm.Engine) Service {
	s := new(service)
	s.db = db
	return s
}

func (s *service) GetOrganizationByID(search string, pager *gintool.Pager) (list []models.Organization, total int64, code int) {
	list, total, code = GetOrganizationByID(s.db, search, pager)
	if code != e.Ok {
		return
	}
	return
}

func (s *service) GetOrganizationByName(search string, pager *gintool.Pager) (list []models.Organization, total int64, code int) {
	list, total, code = GetOrganizationByName(s.db, search, pager)
	if code != e.Ok {
		return
	}
	return
}

func (s *service) GetOrganizationList(search string, pager *gintool.Pager) (list models.OrganizationList, total int64, code int) {
	list, total, code = ListOrganization(s.db, strings.TrimSpace(search), pager)
	if code != e.Ok {
		return
	}
	return
}

func (s *service) GetOrganizationListFromParentOrg(orgID string, pager *gintool.Pager) (list models.OrganizationList, total int64, code int) {
	list, total, code = ListOrganizationFromParentOrg(s.db, orgID, pager)
	if code != e.Ok {
		return
	}
	return
}

func (s *service) CreateOrganization(organization models.Organization) int {
	code := InsertOrganization(s.db, &organization)
	if code != e.Ok {
		return code
	}
	return code
}

func (s *service) UpdateOrganization(organization *models.Organization) int {
	_, code := UpdateOrganization(s.db, organization, []string{"parent_code", "org_name", "contact", "mobile", "created_at", "updated_at"})
	if code != e.Ok {
		return code
	}
	return code
}

func (s *service) GetOrgListByIds(orgCodes []string) (list models.OrganizationList, code int) {
	return getOrgNames(s.db, orgCodes, []string{"code", "org_name"})
}

func (s *service) GetOrgNamesByParent(orgId string) (list models.OrganizationList, code int) {
	return getOrgNamesByParent(s.db, orgId, []string{"code", "org_name"})
}
func (s *service) ReadOrgListByIds(ids []uint32) (models.OrganizationList, error) {
	return orgListByIds(s.db, ids)
}
